<?php

class UserController extends AppController
{
	var $name = 'User';
	var $uses = array();
	var $components = array('Openid');
	
	function login()
	{
		if(isset($_GET['openid-qdb-url']))
		{
			// form login
			$result = $this->Openid->authenticate($_GET['openid-qdb-url'], $this->getCoreUrl() . 'user/login', $this->getCoreUrl());
			
			if(!$result)
				$this->flash('Invalid OpenID identifier. Cannot log in.', '/');
		}
		else if(count($_GET) > 1)
		{
			$response = $this->Openid->getResponse();
			
			if($response)
			{
				$response = $this->Openid->sanitize($response);
				$this->Session->write('openid', $response);
				$this->flash('You are now logged in as <span class="openid-url">' . $response . '</span>.', '/');
				exit();
			}
			
			$this->flash('Error logging in with OpenID: ' . $this->Openid->getError() . '.', '/');
			exit();
		}
	}
	
	function logout()
	{
		$this->Session->delete('openid');
		$this->flash('You are now logged out.', '/');
		exit();
	}
}

?>
